from util.base_driver import driver
from selenium.common.exceptions import NoSuchElementException
from util.log import log
from actions.user import handle_error
from config.config import base_url
import sys



class User(object):

    def __init__(self):
        self.login_name_ID = 'loginname'
        self.login_page_name_ID = "name"
        self.pass_wd_ID = 'pass'
        self.repass_wd_ID = 're_pass'
        self.email_ID = 'email'
        self.submit_CSS = 'input[type="submit"]'
        self.register_link_CSS='a[href="/signup"]'
        self.login_link_CSS = 'a[href="/signin"]'
        self.login_error_msg_CSS = 'div[class="alert alert-error"] > strong'
        self.login_success_name_CSS = 'span[class="user_name"] > a[class="dark"]'

    def go_home_page(self):
        log.info("打开网站首页")
        driver.get(base_url)

    def go_register_page(self):
        log.info("打开网站注册页面")
        driver.find_element_by_css_selector(self.login_link_CSS).click()

    def register(self, loginname=None, passwd=None, repasswd=None, email=None):
        log.info("用户注册：{0}-{1}-{2}-{3}".format(loginname,passwd,repasswd,email))
        try:
            login_name = driver.find_element_by_id(self.login_name_ID)
            login_name.clear()
            login_name.send_keys(loginname)

            pass_wd = driver.find_element_by_id(self.pass_wd_ID)
            pass_wd.clear()
            pass_wd.send_keys(passwd)

            repass_wd = driver.find_element_by_id(self.repass_wd_ID)
            repass_wd.clear()
            repass_wd.send_keys(repasswd)

            email_input = driver.find_element_by_id(self.email_ID)
            email_input.clear()
            email_input.send_keys(email)

            btn = driver.find_element_by_css_selector(self.submit_CSS)
            btn.click()
        except NoSuchElementException:
            log.error('元素定位出错')
            handle_error('注册用户时出错')
            sys.exit()

    def go_login_page(self):
        log.info("进入用户登录页面")
        login_url = driver.find_element_by_css_selector(self.login_link_CSS)
        login_url.click()

    def login(self,loginname=None,passwd=None):
        log.info('用户登录:{0}-{1}'.format(loginname,passwd))
        try:
            login_name = driver.find_element_by_id(self.login_page_name_ID)
            login_name.clear()
            login_name.send_keys(loginname)

            password = driver.find_element_by_id(self.pass_wd_ID)
            password.clear()
            password.send_keys(passwd)

            submit_btn = driver.find_element_by_css_selector(self.submit_CSS)
            submit_btn.click()
        except NoSuchElementException:
            log.error('元素定位出错')
            handle_error('注册用户时出错')
            sys.exit()



    def get_login_result(self):
        """
        登录成功 页面跳转到首页，返回用户名
        登录失败 返回登录错误提示信息
        return: str
        """
        log.info("获取用户登录状态:")
        current_url = driver.current_url
        if "signin" in current_url:
            log.info("用户登录失败")
            error_msg = driver.find_element_by_css_selector(self.login_error_msg_CSS)
            error_msg_text = error_msg.text
            return error_msg_text
        else:
            log.info("用户登录成功")
            success_name = driver.find_element_by_css_selector(self.login_success_name_CSS)
            success_name_text = success_name.text
            return success_name_text

